//
//  TWBVisitorView.swift
//  weiBo
//
//  Created by 杨雷 on 2016/12/12.
//  Copyright © 2016年 Yang.L. All rights reserved.
//

import UIKit

protocol TWBVisitorViewDelegate: NSObjectProtocol {
    
    func loginOperation()
}


class TWBVisitorView: UIView {

    var callBack: (() -> ())?
    
    weak var delegate: TWBVisitorViewDelegate?
    
    private lazy var cycleImageView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    private lazy var maskImageView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var iconImageView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        
        label.font = UIFont.systemFont(ofSize: 13)
        
        label.textColor = UIColor.darkGray
        
        label.textAlignment = .center
        
        label.numberOfLines = 0
        
        return label
        
    }()

    private lazy var registerButton: UIButton = {
        
        let btn = UIButton()
        
        btn.addTarget(self, action: #selector(registerButtonAction), for: .touchUpInside)
        
        btn.setTitle("注册", for: .normal)
        
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white_disable"), for: .normal)
        
        btn.adjustsImageWhenHighlighted = false
        
        return btn
    }()
    
    private lazy var loginButton: UIButton = {
        
        let btn = UIButton()
        
        btn.addTarget(self, action: #selector(loginButtonAction), for: .touchUpInside)
        
        btn.setTitle("登陆", for: .normal)
        
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white_disable"), for: .normal)
        
        btn.adjustsImageWhenHighlighted = false
        
        return btn
    }()
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI () {
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        addSubview(cycleImageView)
        
        addSubview(maskImageView)
        
        addSubview(iconImageView)
        
        addSubview(messageLabel)
        
        addSubview(registerButton)
        
        addSubview(loginButton)
        
        cycleImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(cycleImageView)
        }
        
        iconImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(cycleImageView)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(cycleImageView)
            
            make.top.equalTo(cycleImageView.snp_bottom)
            
            make.width.equalTo(225)
        }
        
        registerButton.snp_makeConstraints { (make) in
            
            make.left.equalTo(messageLabel)
            
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        loginButton.snp_makeConstraints { (make) in
            
            make.right.equalTo(messageLabel)
            
            make.top.equalTo(registerButton)
            
            make.size.equalTo(registerButton)
        }
        
    }
    
    private func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        animation.duration = 20
        
        animation.repeatCount = MAXFLOAT
        
        animation.isRemovedOnCompletion = false
        
        cycleImageView.layer.add(animation, forKey: nil)
        
    }
    
    func updateVisitorInfo(imageName: String?, message: String?) {
        
        if let imgName = imageName, let msg = message {
            
            iconImageView.image = UIImage(named: imgName)
            
            messageLabel.text = msg
            
            cycleImageView.isHidden = true
        } else {
            
            startAnimation()
        }
        
    }
    
    @objc private func registerButtonAction() {
        
        callBack?()
        
        delegate?.loginOperation()
    }
    
    @objc private func loginButtonAction() {
        
        callBack?()
        
        delegate?.loginOperation()
    }
    
}
